﻿using ArtMath.Attribute;
using ArtMath.Component.Line;
using ArtMath.Component.Point;
using ArtMath.Core.Data;
using ArtMath.PointCollection;
using ArtMath.Resource;
using System;
using System.Linq;
using System.Collections.Generic;

namespace ArtMath.Component.Polygon
{
    /// <summary>
    /// 等腰三角形
    /// </summary>
    [Serializable]
    public class IsoscelesTriangle : TriangleBase
    {
        public IsoscelesTriangle(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.dysjx, addToList)
        {

        }
        [MultilingualDisplayName("{0}1", "Isosceles")]
        public override LineSegmentBase Line1
        {
            get
            {
                return base.Line1;
            }

            set
            {
                base.Line1 = value;
            }
        }
        [MultilingualDisplayName("{0}2", "Isosceles")]
        public override LineSegmentBase Line2
        {
            get
            {
                return base.Line2;
            }

            set
            {
                base.Line2 = value;
            }
        }
        [MultilingualDisplayName("Hemline")]
        public override LineSegmentBase Line3
        {
            get
            {
                return base.Line3;
            }

            set
            {
                base.Line3 = value;
            }
        }
        public override void ComponentBuilding(Base component)
        {
            PointD pt;
            switch (OrderIndex)
            {
                case 0:
                    if (component == Line2)
                    {
                        Line2.AllowFix = false;
                        Line2.SpareCoordinate_ = Pad.FixOnCircle(Line2.Point1.Pxy, Line1.Length, Line2.SpareCoordinate);
                        Line2.AllowFix = true;
                        pt = Pad.PxyToXY(Line2.SpareCoordinate);
                        double len = pt.DistanceTo(Line1.Point1.ToPointD());
                        calcCenters(Line1.Point1.ToPointD(), Line2.Point1.ToPointD(), pt);
                        Circumference = Line1.Length + Line2.Length + len;
                        Area = Math.Sqrt(Line1.Length * Line1.Length - len * len / 4) * len / 2;
                    }
                    else
                    {
                        ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}");
                        return;
                    }
                    break;
                case 1:
                    if (component == Line3)
                    {
                        Line3.AllowFix = false;
                        Line3.SpareCoordinate_ = Pad.FixOnCircle(Line1.Point1.Pxy, Line1.Length, Line3.SpareCoordinate);
                        Line3.AllowFix = true;
                        pt = Pad.PxyToXY(Line3.SpareCoordinate);
                        calcCenters(Line1.Point1.ToPointD(), Line3.Point1.ToPointD(), pt);
                        Circumference = Line1.Length * 2 + Line3.Length;
                        Area = Math.Sqrt(Line1.Length * Line1.Length - Line3.Length * Line3.Length / 4) * Line3.Length / 2;
                    }
                    else
                    {
                        ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}");
                        return;
                    }
                    break;
                case 2:
                    if (component == Line1)
                    {
                        Line1.AllowFix = false;
                        Line1.SpareCoordinate_ = Line1.SpareCoordinate.GetProjectivePoint((Line3.Point1.Pxy + Line1.Point1.Pxy) / 2, Pad.kToPk(-1 / Line3.Slope));
                        Line1.AllowFix = true;
                        pt = Pad.PxyToXY(Line1.SpareCoordinate);
                        calcCenters(Line3.Point1.ToPointD(), Line1.Point1.ToPointD(), pt);
                        Circumference = Line1.Length * 2 + Line3.Length;
                        Area = Math.Sqrt(Line1.Length * Line1.Length - Line3.Length * Line3.Length / 4) * Line3.Length / 2;
                    }
                    else
                    {
                        ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}");
                        return;
                    }
                    break;
            }
            base.ComponentBuilding(component);
        }
        public override PointCollectionBase[] GetInPointMovingRange(PointBase movePoint, Base sender = null)
        {
            switch (CountMovablePoints())
            {
                case 1:
                    if (OrderIndex == 1)
                    {
                        if (movePoint == Line1.Point1)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line3.NewMidPoint, -1 / Line3.NewSlope) };
                        }
                        else if (movePoint == Line2.Point1)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line1.Point1.NewXY, Line1.NewLength) };
                        }
                        else if (movePoint == Line3.Point1)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line1.Point1.NewXY, Line2.NewLength) };
                        }
                    }
                    else
                    {
                        if (movePoint == Line1.Point1)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.NewXY, Line2.NewLength) };
                        }
                        if (movePoint == Line2.Point1)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line3.NewMidPoint, -1 / Line3.NewSlope) };
                        }
                        else if (movePoint == Line3.Point1)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.NewXY, Line1.NewLength) };
                        }
                    }
                    break;
            }
            return base.GetInPointMovingRange(movePoint, sender);
        }
        public override void CreateComponents()
        {
            switch (OrderIndex)
            {
                case 0:
                    if (Line1 == null)
                    {
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}1", false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line1.CreateComponents($"{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line2 == null)
                    {
                        Line1.CreateComponents($"{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}2", false);
                        Line2.CreateComponents(Line1.Point2);
                    }
                    else
                    {
                        Line2.CreateComponents($"{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.ToPointD(), Line1.Length) });
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("Hemline"), false) { AllowFix = false };
                        Line3.CreateComponents(Line2.Point2, Line1.Point1);
                        base.CreateComponents();
                    }
                    break;
                case 1:
                    if (Line1 == null)
                    {
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}1", false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line1.CreateComponents($"{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line3 == null)
                    {
                        Line1.CreateComponents($"{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("Hemline"), false);
                        Line3.CreateComponents(Line1.Point2);
                    }
                    else
                    {
                        Line3.CreateComponents($"{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new PointCollection.Circle(Line1.Point1.ToPointD(), Line1.Length) });
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}2", false) { AllowFix = false };
                        Line2.CreateComponents(Line3.Point2, Line1.Point1);
                        base.CreateComponents();
                    }
                    break;
                case 2:
                    if (Line3 == null)
                    {
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("Hemline"), false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line3.CreateComponents($"{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line1 == null)
                    {
                        Line3.CreateComponents($"{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Hemline")}{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}1", false);
                        Line1.CreateComponents(Line3.Point2);
                    }
                    else
                    {
                        Line1.CreateComponents($"{Resources.Translate("Isosceles")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("Isosceles")}1{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new StraightLine(Line3.MidPoint, -1 / Line3.Slope) });
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("Isosceles")}2", false) { AllowFix = false };
                        Line2.CreateComponents(Line1.Point2, Line3.Point1);
                        base.CreateComponents();
                    }
                    break;
            }
        }
    }
}
